Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

297
Visualizações
Passing Python large integers (>256bits) to and from C++ with pybind11

I'm developing a python wrapper for C++ based cryptography scheme where the keys and ciphertext are very large numbers (up to 2048 bits).

In C++, I'm using Intel BigNumber class to store and handle such large numbers.

As Python natively supports "seemingly" infinite bits of integer through objects, I'm having trouble passing such variables as parameters via Python.

  py::class_<BigNumber>(m, "BigNumber")
      .def(py::init<unsigned int>())
      .def(py::init<int>())
      .def(py::init<BigNumber>())
      .def(py::init([](py::list data) {
        size_t length = data.size();
        unsigned int* pData = new unsigned int[length];
        for (int i = 0; i < length; i++) {
          pData[i] = data[i].cast<unsigned int>();
        }
        return std::unique_ptr<BigNumber>(new BigNumber(pData, length));
      }))
      .def(py::init([](py::array_t<unsigned int> data) {
        py::buffer_info buffer_info = data.request();

        unsigned int* pData = static_cast<unsigned int*>(buffer_info.ptr);
        std::vector<ssize_t> shape = buffer_info.shape;
        return std::unique_ptr<BigNumber>(new BigNumber(pData, shape[0]));
      }))

Above portion allows up to 32 bits single variable or a list/array of broken down large integer (each 32bit)

What if I want to take very large integers from Python as an input to fill the BigNumber class, and also what should I do to define a def_property_readonly() function that returns as a Python understandable large integer object?

Edit1: Passing large integer from Python to C++ With the help of BigNumber(const char* s) constructor, it can be done by:

      .def(py::init([](py::object obj){
        std::string s_obj = py::str(obj);
        return std::unique_ptr<BigNumber>(new BigNumber(&s_obj[0]));
      }))

Figuring out how to do reverse, probably std::string to py::object with py::cast?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Answer for first case: Passing very large integer(>256bits) object from Python to C++ with BigNumber

Passing large integer from Python to C++ With the help of BigNumber(const char* s) constructor, it can be done by:

.def(py::init([](py::object obj){
  std::string s_obj = py::str(obj);
  return std::make_unique<BigNumber>(s_obj.c_str());
}))

However, returning to Python with a large integer object is still WIP

I tried returning to python with py::int_ as:

.def("val", [](BigNumber const& self){
  std::string s_hex;
  self.num2hex(s_hex);
  return py::int_(s_hex);
})

However this invokes the same value error when you try

>>> int("0x12345")

in Python.

ValueError: invalid literal for int() with base 10: '0x12345'

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda